package com.risen.common.core.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.risen.common.core.handler.TenantProperties;
import com.risen.common.core.interceptor.RisenTenantHandler;
import com.risen.common.core.interceptor.RisenTenantInterceptor;
import com.risen.common.core.interceptor.PaginationInterceptor;
import com.risen.common.core.interceptor.QueryInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

@Configuration
@EnableConfigurationProperties(TenantProperties.class)
@MapperScan("com.risen.**.mapper.**")
public class MybatisPlusConfig {

    /**
     * 自定义多租户处理器
     *
     * @param tenantProperties 多租户配置类
     * @return TenantHandler
     */
    @Bean
    public RisenTenantHandler bladeTenantHandler(TenantProperties tenantProperties) {
        return new RisenTenantHandler(tenantProperties);
    }

    /**
     * 自定义租户拦截器
     *
     * @param tenantHandler    多租户处理器
     * @param tenantProperties 多租户配置类
     * @return BladeTenantInterceptor
     */
    @Bean
    public RisenTenantInterceptor tenantLineInnerInterceptor(RisenTenantHandler tenantHandler, TenantProperties tenantProperties) {
        RisenTenantInterceptor tenantInterceptor = new RisenTenantInterceptor();
        tenantInterceptor.setTenantLineHandler(tenantHandler);
        tenantInterceptor.setTenantProperties(tenantProperties);
        return tenantInterceptor;
    }


    /**
     * mybatis-plus 拦截器集合
     */
    @Bean
    //@ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor(ObjectProvider<QueryInterceptor[]> queryInterceptors,
                                                         RisenTenantInterceptor tenantInterceptor) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 配置租户拦截器
        interceptor.addInnerInterceptor(tenantInterceptor);
        // 配置分页拦截器
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 配置自定义查询拦截器
        QueryInterceptor[] queryInterceptorArray = queryInterceptors.getIfAvailable();
        if (ObjectUtil.isNotEmpty(queryInterceptorArray)) {
            AnnotationAwareOrderComparator.sort(queryInterceptorArray);
            paginationInterceptor.setQueryInterceptors(queryInterceptorArray);
        }
//        paginationInterceptor.setMaxLimit(mybatisPlusProperties.getPageLimit());
//        paginationInterceptor.setOverflow(mybatisPlusProperties.getOverflow());
        interceptor.addInnerInterceptor(paginationInterceptor);
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}